<?php
if($_GET["code"]=="reset"){
	session_start();
	# Reset the XML file
	include("scripts/constraint.php");
	$sourceData[0] =  SERVERLOCATION. "/data/backup/EKDocument.xml";
	$data[0] = SERVERLOCATION. "/data/EKDocument.xml";
	$sourceData[1] =  SERVERLOCATION. "/data/backup/EKParagraph.xml";
	$data[1] = SERVERLOCATION. "/data/EKParagraph.xml";
	$sourceData[2] =  SERVERLOCATION. "/data/backup/EKParagraphRelationship.xml";
	$data[2] = SERVERLOCATION. "/data/EKParagraphRelationship.xml";
	$sourceData[3] =  SERVERLOCATION. "/data/backup/EKRating.xml";
	$data[3] = SERVERLOCATION. "/data/EKRating.xml";
	$sourceData[4] =  SERVERLOCATION. "/data/backup/EKStatistics.xml";
	$data[4] = SERVERLOCATION. "/data/EKStatistics.xml";
	$sourceData[5] =  SERVERLOCATION. "/data/backup/EKTalk.xml";
	$data[5] = SERVERLOCATION. "/data/EKTalk.xml";
	$sourceData[6] =  SERVERLOCATION. "/data/backup/tree.xml";
	$data[6] = SERVERLOCATION. "/data/tree.xml";
	for($i=0;$i<count($data);$i++){
		if (!copy($sourceData[$i], $data[$i])) {
			echo "Failed to copy $data[$i]<br />";
		} else {
			echo "XML Data Initialization complete! <br />";
		}
	}
	$connect = mysql_connect('localhost',SQLUSER,SQLPASSWD) or die("Can't connect to db! <br />");
	$result = mysql_query("DROP DATABASE IF EXISTS `".PROJECTNAME."`;",$connect) or die("Can't drop db! <br />");
	$result = mysql_query("CREATE DATABASE `".PROJECTNAME."`",$connect) or die("Can't create db! <br />");
	mysql_select_db(PROJECTNAME);
	
	$sqls = array();
	$sqls[] = "
	CREATE TABLE `ekevent` (
	  `id` int(11) NOT NULL auto_increment,
	  `userId` varchar(10) NOT NULL default '0',
	  `elementType` varchar(1) NOT NULL default 'd',
	  `elementId` int(11) NOT NULL default '0',
	  `eventType` varchar(10) NOT NULL default 'create',
	  `updateTime` int(11) NOT NULL default '0',
	  `docId` varchar(11) default NULL,
	  `docTitle` varchar(255) default NULL,
	  PRIMARY KEY  (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	$sqls[] = "
	CREATE TABLE `ekuser` (
	  `id` varchar(10) NOT NULL default '',
	  `password` varchar(20) default NULL,
	  `name` varchar(50) NOT NULL default '',
	  `type` int(11) NOT NULL default '0',
	  `email` varchar(50) default NULL,
	  `selfIntro` text,
	  `exp` int(11) unsigned NOT NULL default '0',
	  `ratingA` double NOT NULL default '0',
	  `ratingAWeight` int(11) NOT NULL default '1',
	  `ratingB` double NOT NULL default '0',
	  `ratingBWeight` int(11) NOT NULL default '1',
	  `enabled` int(1) NOT NULL default '1' COMMENT '0 or 1',
	  PRIMARY KEY  (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	$sqls[] = "CREATE TABLE `ekuser_d` (
	  `userId` varchar(10) NOT NULL default '',
	  `id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`userId`,`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	$sqls[] = "CREATE TABLE `ekuser_p` (
	  `userId` varchar(10) NOT NULL default '',
	  `id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`userId`,`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	$sqls[] = "CREATE TABLE `ekuser_r` (
	  `userId` varchar(10) NOT NULL default '',
	  `id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`userId`,`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	$sqls[] = "CREATE TABLE `ekuser_t` (
	  `userId` varchar(10) NOT NULL default '',
	  `id` int(11) NOT NULL default '0',
	  PRIMARY KEY  (`userId`,`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	foreach($sqls as $sql){
		$result = mysql_query($sql,$connect) or die("Can't initialize db! <br />");
	}
	echo "DB Initialization complete! <br />";
	$anotherSql = "INSERT INTO `ekuser` VALUES ('admin','111111','Administrator',3,NULL,NULL,1000,5,1,5,1,1);";
	if($result = mysql_query($anotherSql,$connect)){
		echo "Super Administrator [admin:111111] has been added! <br />";
	} else {
		echo "Can't add admin! <br />";
	}
	mysql_close();
	$_SESSION["userid"] = "admin";
	echo "[<a href=\"document.php?id=null&method=c\">NEW ROOT</a>]";
} else {
	echo "Be caution!, the follow link put everything into zero! <br />";
	echo "[<a href=\"install.php?code=reset\">Reset Everything</a>]";
}
?>